package vin.pth.example.service;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class LoginTestService {

  @Autowired
  private RestService restService;

  public String loginAdmin() throws JsonProcessingException {
    return login("admin", "admin");
  }

  public String loginTest() throws JsonProcessingException {
    return login("test", "test");
  }

  private String login(String password, String username) throws JsonProcessingException {
    Map<String, String> map = new HashMap<>();
    map.put("username", password);
    map.put("password", username);
    ResponseEntity<String> response = restService.post("/login", map);
    assertEquals(response.getStatusCode(), HttpStatus.OK);
    return response.getBody();
  }

}
